package com.meiyu.live.ui.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.popj.Level;

/**
 * Created by zbl on 2016/8/23.
 */
public class MyLevelActivity extends BaseActivity implements View.OnClickListener {

    private View btn_back;
    private TextView tv_level, tv_level_now, tv_level_next, tv_exp_now, tv_exp_tonext;
    private ProgressBar progressBar;
    private ImageView[] iv_vip_lock;
    private static final int[] level_unlock = {2, 100, 200, 500};

    private long exp, expToNext, currentLevelExp;
    private int level;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mylevel);
        init();
    }

    private void init() {
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        tv_level = (TextView) findViewById(R.id.tv_level);
        tv_level_now = (TextView) findViewById(R.id.tv_level_now);
        tv_level_next = (TextView) findViewById(R.id.tv_level_next);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        tv_exp_now = (TextView) findViewById(R.id.tv_exp_now);
        tv_exp_tonext = (TextView) findViewById(R.id.tv_exp_tonext);
        progressBar.setMax(1000);
        iv_vip_lock = new ImageView[]{
                (ImageView) findViewById(R.id.iv_vip_lock0),
                (ImageView) findViewById(R.id.iv_vip_lock1),
                (ImageView) findViewById(R.id.iv_vip_lock2),
                (ImageView) findViewById(R.id.iv_vip_lock3)
        };

        getData();
    }

    private void update() {
        level = (int) (Math.sqrt(Math.sqrt(exp + expToNext)));
        progressBar.setProgress((int) (currentLevelExp * 1000 / (currentLevelExp + expToNext)));
        tv_level.setText(level + "");
        tv_level_now.setText("V" + level);
        tv_level_next.setText("V" + (level + 1));

        tv_exp_now.setText("我的经验值:" + exp);
        tv_exp_tonext.setText("离升级还差:" + expToNext);

        for (int i = 0; i < iv_vip_lock.length; i++) {
            if (level >= level_unlock[i]) {
                iv_vip_lock[i].setImageResource(R.drawable.ic_vip_normal);
            } else {
                iv_vip_lock[i].setImageResource(R.drawable.ic_vip_locked);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void getData() {
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.UrlParams.PRIVATELEVEL);
        request(b, Level.class, new Response.Listener<Level>() {
            @Override
            public void onResponse(Level level) {
                if (level != null) {
                    exp = level.getExp();
                    expToNext = level.getExpToNext();
                    currentLevelExp = level.getCurrentLevelExp();
                    update();
                }
            }
        });

    }

    @Override
    public void onError() {
        super.onError();
    }

}
